using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;

using Microsoft.SharePoint.Administration;

namespace JobShopsDiscussion_v01_6_App18.Features.JobShopsDiscussion_v01_6_App18
{

    /// <summary>
    /// Этот класс обрабатывает события, возникающие в ходе активации, деактивации, установки, удаления и обновления компонентов.
    /// </summary>
    /// <remarks>
    /// GUID, присоединенный к этому классу, может использоваться при создании пакета и не должен изменяться.
    /// </remarks>

    [Guid("aec71a5d-3134-44cd-b43b-d2d01744a3ca")]
    public class JobShopsDiscussion_v01_6_App18EventReceiver : SPFeatureReceiver
    {

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {

            SPSecurity.RunWithElevatedPrivileges(delegate()
            {

                using (SPSite site = properties.Feature.Parent as SPSite)
                {

                    try
                    {

                        //SPWebApplication webApp = SPWebApplication.Lookup(new Uri(@"http://localhost/shops"));
                        //SPServer srv = new SPServer("S-WSS02");

                        // delete preview job item
                        foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
                        {
                            if (
                                    job.Name == "JobShopsDiscussion_v1"
                                || job.Name == "JobShopsDiscussion_v2"
                                || job.Name == "JobShopsDiscussion_v3"
                                || job.Name == "JobShopsDiscussion_v4"
                                || job.Name == "JobShopsDiscussion_v5"
                                || job.Name == "JobShopsDiscussion_v6"
                                || job.Name == "JobShopsDiscussion_v7"
                                )
                            {
                                job.Delete();
                            }
                        }
                        JobShopsDiscussion_JobClass_v01_6 listLoggerJob =
                            //new JobShopsDiscussion_JobClass_v01_6("JobShopsDiscussion_v7", webApp);
                            new JobShopsDiscussion_JobClass_v01_6("JobShopsDiscussion_v7", site.WebApplication);
                        SPDailySchedule schedule = new SPDailySchedule();
                        schedule.BeginHour = 1;
                        schedule.BeginMinute = 0;
                        schedule.BeginSecond = 0;
                        schedule.EndHour = 1;
                        schedule.EndMinute = 59;
                        schedule.EndSecond = 59;

                        listLoggerJob.Schedule = schedule;
                        listLoggerJob.Update();

                    }
                    catch (Exception err)
                    {
                    }
                }

            });
                                
        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {

            SPSecurity.RunWithElevatedPrivileges(delegate()
            {

                using (SPSite site = new SPSite(@"http://localhost/shops"))
                {
                    foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
                    {
                        if (
                               job.Name == "JobShopsDiscussion_v1"
                            || job.Name == "JobShopsDiscussion_v2"
                            || job.Name == "JobShopsDiscussion_v3"
                            || job.Name == "JobShopsDiscussion_v4"
                            || job.Name == "JobShopsDiscussion_v5"
                            || job.Name == "JobShopsDiscussion_v6"
                            || job.Name == "JobShopsDiscussion_v7"
                            )
                        {
                            job.Delete();
                        }
                    }
                }

            });

        }

        // Раскомментируйте ниже метод для обработки события, возникающего после установки компонента.

        //public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        //{
        //}


        // Раскомментируйте ниже метод для обработки события, возникающего перед удалением компонента.

        //public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        //{
        //}

        // Раскомментируйте ниже метод для обработки события, возникающего при обновлении компонента.

        //public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, System.Collections.Generic.IDictionary<string, string> parameters)
        //{
        //}

    }
}
